<template>
    <div class="qingwu">
        <div class="admin_table_page_title">代理商列表</div>
        <div class="unline underm"></div>

        <estate-search :searchConfig="searchConfig" @searchParams="search"></estate-search>

        <div class="admin_table_handle_btn">
            <a-button @click="$router.push('/Estate/platform_agent/form')" type="primary" icon="plus">添加</a-button>
            <a-button class="admin_delete_btn" type="danger" icon="delete" @click="del">批量删除</a-button>
        </div>
        <div class="admin_table_list">
            <a-table :columns="columns" :data-source="list" :pagination="false"
                     :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }" row-key="id">
                <span slot="action" slot-scope="rows">
                    <a-button icon="form" @click="$router.push('/Estate/platform_agent/form/'+rows.id)">编辑</a-button>
                </span>

                <span slot="status" slot-scope="rows">
                    <a-switch v-if="rows.status == 1" checked-children="正常" un-checked-children="禁用"
                              @change="onChangeStatus(rows.id)" default-checked/>
                    <a-switch v-else checked-children="正常" @change="onChangeStatus(rows.id)" un-checked-children="禁用"/>
                </span>

            </a-table>
            <div class="admin_pagination" v-if="total>0">
                <a-pagination v-model="params.page" :page-size.sync="params.limit" :total="total" @change="onChange"
                              show-less-items/>
            </div>
        </div>
    </div>
</template>

<script>
    import estateSearch from '@/components/estate/search'

    export default {
        components: {estateSearch},
        props: {},
        data() {
            return {
                params: {
                    page: 1,
                    limit: 30,
                },
                total: 0, //总页数
                searchConfig: [
                    {label: '代理姓名', name: 'agent_name', type: 'text'},
                    {label: '代理商手机', name: 'agent_mobile', type: 'text'},
                    {label: '登陆账号', name: 'login_account', type: 'text'},
                ],
                selectedRowKeys: [], // 被选择的行
                columns: [
                    {title: '代理姓名', dataIndex: 'agent_name'},
                    {title: '代理商手机', dataIndex: 'agent_mobile'},
                    {title: '登陆账号', dataIndex: 'login_account'},
                    {title: '地址', dataIndex: 'full_address'},
                    {title: '状态', scopedSlots: {customRender: 'status'}, width: 150},
                    {title: '添加时间', dataIndex: 'create_time'},
                    {title: '操作', key: 'id', fixed: 'right', scopedSlots: {customRender: 'action'}},
                ],
                list: []
            };
        },
        watch: {},
        computed: {},
        methods: {
            // 选择框被点击
            onSelectChange(selectedRowKeys) {
                this.selectedRowKeys = selectedRowKeys;
            },
            // 选择分页
            onChange(e) {
                this.params.page = e;
                this.onload();
            },
            //状态
            onChangeStatus(id) {
                this.$get(this.$api.statusPlatformAgent + '?id=' + id).then(res => {
                    if (res.code == 200) {
                        this.$message.success('成功');
                    } else {
                        this.$message.error(res.msg)
                    }
                });
            },
            // 删除
            del() {
                if (this.selectedRowKeys.length == 0) {
                    return this.$message.error('未选择数据。');
                }
                this.$confirm({
                    title: '你确定要删除选择的数据？',
                    content: '确定删除后无法恢复.',
                    okText: '是',
                    okType: 'danger',
                    cancelText: '取消',
                    onOk: () => {
                        let ids = this.selectedRowKeys.join(',');
                        this.$delete(this.$api.electricPlatformAgent + '/' + ids).then(res => {
                            if (res.code == 200) {
                                this.onload();
                                this.$message.success('删除成功');
                            } else {
                                this.$message.error(res.msg)
                            }
                        });

                    },
                });
            },
            onload() {
                this.$get(this.$api.electricPlatformAgent, this.params).then(res => {
                    this.total = res.data.total;
                    this.list = res.data.data;
                });
            },
            // 重置
            reset() {
                location.reload();
            },
            // 搜索
            search(params) {
                let page = this.params.page;
                let per_page = this.params.limit;
                this.params = params;
                this.params.page = page;
                this.params.limit = per_page;
                this.onload();
            },
        },
        created() {
            this.onload();
        },
        mounted() {
        }
    };
</script>
<style lang="scss" scoped>

</style>
